package Servlet;

import dao.MyConnection;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.PreparedStatement;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class changeStudentStatusServlet extends HttpServlet {

    @Override
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setHeader("Cache-Control", "no-cache");
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out = response.getWriter();
        int type = Integer.parseInt(request.getParameter("type"));
        String StudentAccount = request.getParameter("StudentAccount");
        MyConnection myCon = new MyConnection();
        myCon.getConnection();
        int res = -1;
        try {
            if (type == 1) {
                PreparedStatement ps = myCon.connection.prepareStatement("update student set borrowStatus=1 where studentaccount=?");
                ps.setString(1, StudentAccount);
                res = ps.executeUpdate();
                ps.close();
            } else {
                PreparedStatement ps = myCon.connection.prepareStatement("update student set borrowStatus=0 where studentaccount=?");
                ps.setString(1, StudentAccount);
                res = ps.executeUpdate();
                ps.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            myCon.closeConnection();
        }
        out.print(res);
    }
}
